package de.tum.beilmeiner.im.client;


import java.util.Map.Entry;

import javax.servlet.http.HttpSession;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.TabPanel;

import de.novanic.eventservice.client.event.Event;
import de.novanic.eventservice.client.event.RemoteEventService;
import de.novanic.eventservice.client.event.RemoteEventServiceFactory;
import de.novanic.eventservice.client.event.domain.DomainFactory;
import de.tum.beilmeiner.im.client.bundle.ChatRoomBundle;
import de.tum.beilmeiner.im.client.controller.ChatRoomController;
import de.tum.beilmeiner.im.client.controller.ContactListController;
import de.tum.beilmeiner.im.client.controller.LoginWindowController;
import de.tum.beilmeiner.im.client.controller.LogoutWindowController;
import de.tum.beilmeiner.im.client.event.LoginEvent;

import de.tum.beilmeiner.im.client.view.ChatRoomWindow;
import de.tum.beilmeiner.im.client.view.ContactListWindow;
import de.tum.beilmeiner.im.client.view.LoginWindow;
import de.tum.beilmeiner.im.client.view.LogoutWindow;
import de.tum.beilmeiner.im.shared.model.ChatRoom;
import de.tum.beilmeiner.im.shared.model.UUID;
import de.tum.beilmeiner.im.shared.model.User;



/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class InstantChat implements EntryPoint {
	
	private UserSessionHandler userhandler = null;
	TabPanel chatRoomPanel = null;
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		System.out.println(this);
		
		userhandler = new UserSessionHandler();
		/**
		 * 
		 * Add LoginWindow to RootPanel to start..
		 *
		 */
		switchToLoginView();
		
		/**
		 * multiple chatrooms available during creation...
		 */
	
	
	}

	public void switchToLoginView()
	{
		
		RootPanel.get().clear();
		LoginWindow loginWindow = new LoginWindow();
		LoginWindowController loginController = new LoginWindowController(loginWindow, userhandler, this);
		RootPanel.get().add(loginWindow);
	}

	public void switchToMainView() {
		
		RootPanel.get().clear();
		ContactListWindow contactListWindow = new ContactListWindow();
		ContactListController contactListController = new ContactListController(contactListWindow, userhandler, this);
		
		LogoutWindow LogoutWindow = new LogoutWindow();
		LogoutWindowController logoutWindowController = new LogoutWindowController(LogoutWindow, userhandler, this);
		
		chatRoomPanel = new TabPanel();
		chatRoomPanel.setSize("800px", "600px");
		chatRoomPanel.setAnimationEnabled(true);
		RootPanel.get().add(contactListWindow);
		RootPanel.get().add(chatRoomPanel);
		chatRoomPanel.setVisible(false);
		RootPanel.get().add(LogoutWindow);
	}

	public void switchViewToChatWindow(ChatRoomBundle chatRoomBundle) {
		System.out.println("switch to ChatWindow Tab..!");
		
	}

	public void createNewChatWindow(ChatRoomBundle chatRoomBundle) {
		
		
		System.out.println(chatRoomBundle.getChatRoom().getChatPartners().size());
		String titleUser = "";
		for (Entry<String, User> user : chatRoomBundle.getChatRoom().getChatPartners())
			titleUser = titleUser +" / " +user.getValue().getName();
			
				chatRoomPanel.add(chatRoomBundle.getChatRoomWindow(), titleUser, false);
				chatRoomPanel.setVisible(true);
		
	
	System.out.println("generate ChatWindow Tab..!");
	}

	public void switchToLogoutView() {
		Window.Location.reload();
		RootPanel.get().clear();
		LoginWindow loginWindow = new LoginWindow();
		LoginWindowController loginController = new LoginWindowController(loginWindow, userhandler, this);
		RootPanel.get().add(loginWindow);
		
	}






}
